package stackOverflow;

import java.util.Collection;
import java.util.HashMap;

public class PostVotesContainer {
    public class Votes{
	private int up;
	private int down;
	private int postID;
	
	public Votes(int postID, int up, int down) {
	    this.up	= up;
	    this.down	= down;
	    this.postID	= postID;
	}
	
	public int getPostID()	{return postID;}
	public int getUp() 	{return up;}
	public int getDown() 	{return down;}
	public void incUp() 	{this.up++;}
	public void incDown() 	{this.down++;}
    }
    
    private HashMap<Integer, Votes> conteiner;
    
    public PostVotesContainer(){
	conteiner = new HashMap<Integer, PostVotesContainer.Votes>();
    }
    public boolean isExists(int postID){
	return conteiner.containsKey(postID);
    }
    
    /**
     * add an up vote.
     * if post not in conteiner - add it and then add a up vote
     * @param postID
     */
    public void incUp(int postID){
	if (isExists(postID)){
	    conteiner.get(postID).incUp();
	} else {
	    conteiner.put(postID, new Votes(postID, 1, 0));
	}
    }
    
    /**
     * add an down vote.
     * if post not in conteiner - add it and then add a down vote
     * @param postID
     */
    public void incDown(int postID){
	if (isExists(postID)){
	    conteiner.get(postID).incDown();
	} else {
	    conteiner.put(postID, new Votes(postID, 0, 1));
	}
    }
    
    public Collection<Votes> getAllVotes(){
	return conteiner.values();
    }
}
